package com.geostar.gmp.snmp;

import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.PDU;
import org.snmp4j.event.ResponseEvent;
import com.geostar.gmp.dao.ILogDao;
import com.geostar.gmp.exception.PDUExecption;
import com.geostar.gmp.model.RecordInfo;
import com.geostar.gmp.model.factory.SessionFactory;

/**
 * 文件名:AbstractGetStatus.java
 * 功能: 获取电脑状态的抽象类
 * 目前支持方法
 * --------------------------------------
 * 初始版本 
 * 作者: yesong
 * 时间: Apr 4, 2014
 */
public abstract class AbstractGetStatus {

	private static Logger logger = LoggerFactory.getLogger(AbstractGetStatus.class);
	/** 根据IP查询到的节点信息 **/
	private Element element;
	/** IP **/
	private String ip;
	/**
	 * 目的:
	 * @param comp
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PDU getStatus(String comp){
		Element element_cpu = element.elementByID(comp);
		List<String> oidList = new ArrayList<String>();
		if(element_cpu!=null){
			List<Element> oidElements = element_cpu.elements("oid");
			for (Element element_oid : oidElements) {
				oidList.add(element_oid.getTextTrim());
			}
		}
		ResponseEvent respEvent = SnmpGet.snmpGetList(ip,oidList);
		PDU response = respEvent.getResponse();
		if (respEvent == null || response == null) {
			logger.info(">>>>>>>>>>>>>>>>>>response is null, request time out<<<<<<<<<<<<<<<<<<<<<<");
			return null;
		} else {
			return response;
		}
	}
	
	public boolean isLinked() throws PDUExecption{
		if(SnmpGet.isLinkd(ip)){
			return true;
		}else{
			SqlSession session = SessionFactory.createSession();
			ILogDao logDao = session.getMapper(ILogDao.class);
			RecordInfo info = new RecordInfo(ip+"无法连接");
			logDao.insert_record(info);
			session.commit();
			SessionFactory.closeSession(session);
			return false;
		}
	}
	
	public Element getElement() {
		return element;
	}
	public void setElement(Element element) {
		this.element = element;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	
	public static void main(String[] args) {
//		JsonGenerator
	}
}
